<?php

class CpsetingsController extends Core_Controller_Action 
{
	private $_setings;
	
	public function init() {
	
		$this->_setings = new Settings();
	}
	
	public function indexAction() {
		$this->_forward( 'upload', 'cpsetings' );
	}
	
	//=========kontakt=======================================
	
	public function listAction() 
	{
		$_contact = new Contact();
		$page = intval( $this->_request->getParam( 'page', 1 ) );
		$pagerLayout = $_contact->getListWithPager($page,$this->view->baseUrl());
		$this->view->list = $pagerLayout->execute();
		$this->view->pager = $pagerLayout;
		$this->view->yih2 = 'Kontakt';
	}

	public function listaAction() {
		$page = intval( $this->_request->getParam( 'page', 1 ) );
		$type = intval( $this->_request->getParam( 'type', 0 ) );
		$lista = new Settings();
		$pagerLayout = $lista->getListWithPager( $type, $page, $this->view->baseUrl() );

		$this->view->list = $pagerLayout->execute();
		$this->view->pager = $pagerLayout;

	}	
	
	public function deleteAction() {
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();

		$_contact = new Contact();
		
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		
		if( $id > 0 ) {
			$_contact->deleteItem( $id );
			$this->_redirect( 'cpsetings/list/oper/4' );
		} else {
			$this->_redirect( 'cpsetings/list/oper/3' );
		}
	}
	
	public function itemaAction() {
	
		$id = intval( $this->_request->getParam( 'id', 0 ) );

		$_contact = new Settings();

		$Galeries = new Galeries();
		$this->view->galleriesList = $Galeries->getList();

		if( $this->_request->isPost() ) {
		
			$post = $this->_request->getPost();

			if( empty( $post['title_pl'] ) ) {
				$this->view->error = 'Podaj nazwę';
				
			} else {

				if( $id ) {
					
						$item = $_contact->getItema( $id )->getFirst();
						foreach( $post as $key => $value ) {
		
						
								$item->galeries_g_id = $post['galeries_g_id'];
							
						}
						$item->save();

						$this->_redirect( 'cpsetings/lista/type/' . $id );
						
					
				} else {
					$item = new Contact();
					foreach( $post as $key => $value ) {
	
						if( $key != 'contact' ) {
							$item->{$key} = $value;
						}
					}
					$item->save();

					$this->_redirect( 'cpsetings' );
				}
			}

		} else {
			if( $id ) 
			{
				$item = $_contact->getItema( $id )->toArray();
				$itema = $_contact->getItema( $id )->getFirst();
				$this->view->title_pl = $itema->title_pl;
				$this->view->item = $item[0];
			}
		
		}
		
	}
	
	public function itemAction() {
	
		$id = intval( $this->_request->getParam( 'id', 0 ) );

		$_contact = new Contact();
		$this->view->categories = array(0);

		$Galeries = new Galeries();
		$this->view->galleriesList = $Galeries->getList();

		if( $this->_request->isPost() ) {
		
			$post = $this->_request->getPost();

			if( empty( $post['city_name'] ) ) {
				$this->view->error = 'Podaj nazwę';
				
				$this->view->ly = '19.954605';
				$this->view->lx = '50.064192';
				
			} else {

				if( $id ) {
					
						$item = $_contact->getItem( $id )->getFirst();
						foreach( $post as $key => $value ) {
		
							if( $key != 'contact' ) {
								$item->{$key} = $value;
							}
						}
						$item->save();

						$this->_redirect( 'cpsetings/list/oper/2' );
						
					
				} else {
					$item = new Contact();
					foreach( $post as $key => $value ) {
	
						if( $key != 'contact' ) {
							$item->{$key} = $value;
						}
					}
					$item->save();

					$this->_redirect( 'cpsetings/list/oper/1' );
				}
			}

		} else {
			if( $id ) 
			{
				$item = $_contact->getItem( $id )->toArray();
				$this->view->item = $item[0];
				$loc = explode(';',$item[0]['city_loc']);

				$this->view->ly = $loc[1];
				$this->view->lx = $loc[0];
				
			}
			else
			{
				$this->view->ly = '19.954605';
				$this->view->lx = '50.064192';
				
			}
		
		}
		
		$this->view->yih2 = 'Kontakt';
	}
	
	
	//=========================================================
	
	public function uploadAction()
	{
		$this->view->type = intval( $this->_request->getParam( 'type', 1 ) );
		if($this->view->type==1) {
			$this->view->plik = 'cennikiem';
		} else {
			$this->view->plik = 'katalogiem';
		}

		if($this->_request->isPost()) {
			if($_FILES['pliczek']['name']!='') {
				$file = file_get_contents( $_FILES['pliczek']['tmp_name'] );
				
				$nazwa = md5( $file ). '.pdf';
				copy($_FILES['pliczek']['tmp_name'] , './public/uploads/'.$nazwa);
				
				chmod( './public/uploads/'.$nazwa, 0777 );

				
				$obiekt = $this->_setings->getItemById(intval( $this->_request->getParam( 'type', 1 ) ));
				$obiekt->value = $nazwa;
				$obiekt->save();
				
				$this->_redirect( '/cpsetings/upload/type/' . $this->view->type . '/oper/2' );
			}
		}
	}
	
	//=============================================================
	
	public function headerAction()
	{
		$this->view->menu = array(
			array('name' => 'Carnauba', 'id'=>'1'),
			array('name' => 'Sklep', 'id'=>'2'),
			array('name' => 'Usługi', 'id'=>'3'),
			array('name' => 'Współpraca', 'id'=>'4'),
			array('name' => 'Klub Carnauba', 'id'=>'5'),
			array('name' => 'Akademia autodetailingu', 'id'=>'6'),
			array('name' => 'Kontakt', 'id'=>'7')
			);
			
		$this->view->yih2 = 'Header';
	}
	
	public function editAction()
	{
		$header = new Header();	
		
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		$opper = intval( $this->_request->getParam( 'opper', 0 ) );
		
		if($opper==1)
		{
			$post = $this->_request->getPost();
			
			$i = 1;
			do
			{
			if(strcmp('',$post['img'.$i]))
			{
				
				$headerItem = new Header();
	
				if(strcmp('',$post['imgID'.$i]))
				{
				
				$item = new Header();	
				$item2 = $item->getItemByID($post['imgID'.$i])->getFirst();
								
				$item2->img = $post['img'.$i];
				$item2->menu = $id;
				$item2->number = $i; 
				
				$item2->save();
				
				
				}
				else
				{	
				$headerItem->img = $post['img'.$i];
				$headerItem->menu = $id;
				$headerItem->number = $i; 
				
				$headerItem->save();
				}
			}
			else
			{
				if(!strcmp('del',$post['imgDEL'.$i]))
				
				{
				
					$headerDel = new Header();
					$tmp = $headerDel->deleteByID($post['imgID'.$i]);
				}
			}
			$i++;
			}
			while($i <= 12);
			
			
		
		$this->_redirect( 'cpsetings/edit/id/' . $id );
		}

		$item = $header->getItemByCategory($id)->toArray();

		$this->view->item = $item;
		
		$item2 = array();
		
		for($i=0;$i<12;$i++)
			$item2[$i] = 0;
		for($i=0;$i < count($item);$i++)
		{
			$item2[$item[$i]['number']-1] = $i+1;
			
		}
		
		$this->view->item2 = $item2;
		//print_r($item2);
			
			
	    $this->view->header = $id;
		$this->view->yih2 = 'Header';
	}
	
	public function generateitemAction()
	{
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		$post = $this->_request->getPost();

		$images = new Core_Images( getcwd() . $this->config['dirs']['upload'] . $post['img'] );
		$images->thumb( 390, 312 );
		$images = new Core_Images( getcwd() . $this->config['dirs']['upload'] . $post['img'] );
		$images->thumb( 170, 170 );

		echo Zend_Json::encode(array(
			'status' => 'OK',
			'img' => $this->view->File( $post['img'], 170 )
		));
	}
	
	public function shopMiotaczAction()
	{
		$Shop = new News();
		$miotacz = $Shop->getItemBySpeciecId( 10 );
		$this->view->gelleryid = $miotacz->galeries_g_id;
	}
	
}